const express = require('express')
const db = require('./server/config/database')

const app = express()

app.get('/test-novels', async (req, res) => {
  try {
    console.log('开始查询小说数据...')

    const novels = await db.query(`
      SELECT * FROM novels 
      WHERE status IN ('ongoing', 'completed', 'suspended')
      ORDER BY updated_at DESC
      LIMIT 5
    `)

    console.log(`查询到 ${novels.length} 本小说`)

    const formattedNovels = novels.map(novel => ({
      ...novel,
      tags: novel.tags ? JSON.parse(novel.tags) : []
    }))

    res.json({
      success: true,
      data: formattedNovels,
      total: formattedNovels.length
    })
  } catch (error) {
    console.error('查询失败:', error)
    res.status(500).json({
      success: false,
      message: '查询失败: ' + error.message
    })
  }
})

const PORT = 3001
app.listen(PORT, () => {
  console.log(`测试服务器运行在 http://localhost:${PORT}`)
  console.log('测试地址: http://localhost:3001/test-novels')
})
